<?php

if( !defined("IN_ELCIUS") ) exit;

$PageData['Progression'] = array(
	'Ranks' => array(),
	'Kills' => array(),
);

// TODO: add support for getting ranks independent of guild size?
// TODO: double check data validation

function WP_Quality( $v ){
	if( $v <= 10 ) return 'Legendary';
	elseif( $v <= 100 ) return 'Epic';
	elseif( $v <= 1000 ) return 'Superior';
	elseif( $v <= 2500 ) return 'Uncommon';
	return 'Common';
}

// blank data
$progression['Kills'] = array();
foreach( $Settings['TierInfo']['Bosses'] as $i => $bossName ){
	$progression['Kills'][$i] = array(
		'Index' => $i,
		'Name' => $bossName,
		'KillDate' => '',
		'Status' => 'Alive',
		'Rank' => array(
			'World' => '',
			$Settings['Guild']['Region'] => '',
			'Realm' => '',
		),
		'Image' => preg_replace("/[\s]|[\']|[\-]/","",$bossName).'.png', // TODO: maybe just replace this with 'NameSafe' or something
	);
}



$WowProgressHTML = ExternalContent::Fetch(
	'http://www.wowprogress.com/guild/'.
	strtolower($Settings['Guild']['Region']).
	'/'.strtolower($Settings['Guild']['Realm']).
	'/'.strtolower($Settings['Guild']['Name']).
	'/rating.tier'.$Settings['TierInfo']['Number'].
	'_'.$Settings['Guild']['Size'],
	'WoWProgress - World of Warcraft Rankings'
);


// pull the ranking info from the page
if( preg_match( '/<div class="guildStats">(.*?)<\/div>/i', $WowProgressHTML, $guildRanking ) ) {
	$guildRanking = explode( "</dl>", $guildRanking[1] );
	unset($guildRanking[5]);
	
	// TODO: add the rest of the locales
	$WP_RegionLinks = array(
		'World' => 'rating.tier14_10',
		'US' => 'pve/us/rating.tier14_10',
		'Oceanic' => 'pve/oc/rating.tier14_10',
		'Realm' => 'pve/'.strtolower($Settings['Guild']['Region']).'/'.strtolower($Settings['Guild']['Realm']).'/rating.tier14_10',
	);
	
	foreach( $guildRanking as $i => $value ){
		$value = preg_replace( '/<(.*?)>/i', '', $value );
		$value = explode( ": ", $value );
		$value[0] = ucfirst($value[0]);
		
		if( $value[0] != 'Score' ){
			
			$progression['Ranks'][] = array(
				'Lable' => $value[0],
				'Value' => $value[1],
				'Quality' => WP_Quality( $value[1] ),
				'Link' => isset( $WP_RegionLinks[ $value[0] ] ) ? 'http://www.wowprogress.com/'.$WP_RegionLinks[ $value[0] ] : '',
			);
			
		}
		
	}
}

// get boss kill times & ranks
if( preg_match( '/<table class="rating">(.*?)<\/table>/is', $WowProgressHTML, $bossRankingTable ) ){
	$bossRankings = explode( '</tr>', $bossRankingTable[1] );
	unset($bossRankings[0]);
	
	foreach( $bossRankings as $raw ){
		
		if( !trim($raw) ) continue;
		
		$raw = explode("</td>", str_replace( '<td>', '', $raw ) ); // split kill info into parts
		
		preg_match( '/>(H: |)([a-zA-Z0-9\'\-\s]+)( \([0-9]+\)|)</i', $raw[0], $killInfo ); // extract boss name and difficulty
		
		$bossIndex = array_search( $killInfo[2], $Settings['TierInfo']['Bosses'] ); // match against white-list of bosses
		
		if( ( $bossIndex !== false ) and $progression['Kills'][$bossIndex]['Status'] == 'Alive' ){
			// boss is valid and not already filled (heroic kills get filled first (TODO: confirm))
			
			// extract kill time
			preg_match( '/([a-zA-Z0-9,:\s]+)(" class="datetime">|<\/span>)/i', $raw[1], $killTime );
			// TODO: convert kill time to timestamp, and reformat using $Settings['TimeFormat']
			
			// update kill data
			$progression['Kills'][$bossIndex]['KillDate'] = $killTime[1];
			$progression['Kills'][$bossIndex]['Status'] = 'Dead'.(empty($killInfo[1])?'N':'H');
			$progression['Kills'][$bossIndex]['Rank'] = array(
				'World' => clean( $raw[3], "0-9" ),
				$Settings['Guild']['Region'] => clean( $raw[4], "0-9" ),
				'Realm' => clean( $raw[5], "0-9" ),
			);
			/*
			$progression['Kills'][$bossIndex]['RankQuality'] = array(
				'World' => WP_Quality( $progression['Kills'][$bossIndex]['Rank']['World'] ),
				$Settings['Guild']['Region'] => WP_Quality( $progression['Kills'][$bossIndex]['Rank'][ $Settings['Guild']['Region'] ] ),
				'Realm' => WP_Quality( $progression['Kills'][$bossIndex]['Rank']['Realm'] ),
			);
			*/
			
		}
		
	}
	
}

$PageData['Progression'] = $progression;

?>